package com.lmabbe.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.objects.converter.TenantMiniappConverter;
import com.lmabbe.tenant.objects.dto.TenantMiniappDTO;
import com.lmabbe.tenant.objects.entity.TenantMiniapp;
import com.lmabbe.tenant.objects.query.TenantMiniappQuery;
import com.lmabbe.tenant.objects.vo.TenantMiniappVO;
import com.lmabbe.tenant.service.TenantMiniappService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户小程序配置表管理
 *
 * @author lmabbe
 */
@Api(tags = "租户小程序配置表管理")
@RestController
@RequestMapping("tenantMiniapp")
@RequiredArgsConstructor
public class TenantMiniappController {

    private final TenantMiniappService tenantMiniappService;
    private final TenantMiniappConverter tenantMiniappConverter;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<TenantMiniappVO>> list() {
        List<TenantMiniapp> list = tenantMiniappService.list();
        return R.success(tenantMiniappConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<TenantMiniappVO>> list(TenantMiniappQuery query) {
        IPage page = tenantMiniappService.page(query.createPage(), query.createQuery());
        page.setRecords(tenantMiniappConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<TenantMiniappVO> getById(@PathVariable Long id) {
        return R.success(tenantMiniappConverter.entity2Vo(tenantMiniappService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody TenantMiniappDTO dto) {
        this.tenantMiniappService.save(tenantMiniappConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{miniappId}")
    public R update(@RequestBody TenantMiniappDTO dto, @PathVariable Long miniappId) {
        this.tenantMiniappService.updateById(tenantMiniappConverter.dto2Entity(dto, miniappId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.tenantMiniappService.removeById(id);
        return R.success();
    }

}